/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package events.animation;

import gui.SnazzySpritesGUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import snazzysprites.SnazzySprites;
import state.SnazzySpritesStateManager;
import static snazzysprites.SnazzySpritesSettings.*;
import state.Animation;
import state.SnazzySpritesState;
import state.SpriteType;

/**
 * Adds a new Animation to the current SpriteType. The user is prompted for 
 * the name of the new animation.
 * @author Andrew Gratta <andrew.gratta@stonybrook.edu>
 */
public class AddAnimationHandler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        SnazzySprites singleton = SnazzySprites.getSnazzySprites();
        SnazzySpritesStateManager manager = singleton.getStateManager();
        SnazzySpritesGUI gui = singleton.getGUI();
        String name = JOptionPane.showInputDialog(gui, ANIMATION_NAME_REQUEST_TEXT,
                ANIMATION_NAME_REQUEST_TITLE_TEXT, JOptionPane.QUESTION_MESSAGE);
        name = name.trim();
        if (!name.isEmpty() && name.length() < 20 && manager.getSpriteType().getAnimation(name) == null)
        {
            Animation anim = new Animation(name, DEFAULT_ANIMATION_WIDTH, DEFAULT_ANIMATION_HEIGHT, true);
            SpriteType currentSprite = manager.getSpriteType();
            currentSprite.addAnimation(anim);
            manager.setAnimation(name);
            gui.setAnimationComboBoxItems(manager.getAnimationNames());
            manager.setState(SnazzySpritesState.SELECT_POSE_STATE);
        }
        
    }

}
